<?php

/** 
 * 水价格模型
 */
class Water_price_mdl extends MY_Model {
	
	/**
	 * 水价配置文件路径
	 * @var string
	 */
	private $_cfgPath;
	
	/**
	 * 默认水价
	 * @var array
	 */
// 	private $_defPrice = array(
// 			BUY_MODE_NORMAL=>array(
// 					'hot'=>8,	
// 					'cold'=>2,
// 			),
// 			BUY_MODE_ONCE =>array(
// 					'hot'=>32,
// 					'cold'=>8,
// 			),
// 	);

	//默认水价
	private $_defPrice = array(
			array (
				    'hot' => 
				    array (
				      'a' => 1,
				      'p1' => 1,
				      'p2' => 1,
				    ),
				    'cold' => 
				    array (
				      'a' => 1,
				      'p1' => 1,
				      'p2' => 1,
				    ),
					//脉冲热水
					'phot' =>
					array (
							'a' => 1,
					),
					//脉冲冷水
				    'pcold' =>
					array (
							'a' => 1,
					),
				    'once' => 300,
				    'version' => 1,
					'sid' =>0,
			)
	);
	/**
	 * 水价
	 * @var array
	 */
	private $_price = NULL;
	
	public function __construct() {
		parent::__construct ();
// 		$this->_cfgPath = APPPATH.'config/waterPrice.php';
		$this->_cfgPath = APPPATH.'config/waterPrice.json';
	}
	
	/**
	 * 设置水价
	 * @return bool
	 */
	public function setPrices($data){
// 		return (bool)file_put_contents($this->_cfgPath, '<?php return '.var_export($data,TRUE).';');
		if ( empty($data) || !is_array($data)){
			return false;
		}
		$str = json_encode($data);
		return (bool)file_put_contents($this->_cfgPath,$str);
	}
	
	/**
	 * 获取水价
	 * @param int $mode		消费模式
	 * 				<p>BUY_MODE_NORMAL : 普通模式
	 * 				<p>BUY_MODE_ONCE   : 按次消费
	 * @param string $type	水类型
	 * 				<p>'hot'  : 热水
	 * 				<p>'cold' : 冷水 
	 * @return array | int
	 * 				<p>$mode不为空且$type为空,返回该模式的价格(一维数组)
	 * 				<p>$mode不为空且$type不为空,返回该模式该类型的水价(int)
	 * 修改后直接返回该学校所有价格
	 */
	public function getPrice( $mode, $type = NULL,$schoolId=1   ){
		
		$this->loadPrice();
		$price = $this->_price;
		$price = isset($price[$schoolId])?$price[$schoolId]:$price[0];
		return $price;
		
// 		$price = isset($this->_price[$mode]) ? $this->_price[$mode] : array('hot'=>9999,'cold'=>9999);
// 		if ( ! $type ) {
// 			return $price;
// 		}
// 		return isset($price[$type]) ? $price[$type] : 9999;
	}
	
	public function getPriceList(){
		$this->loadPrice();
		$price = $this->_price;
		return $price;
	}
	
	private function loadPrice(){
		if( NULL === $this->_price ){
			$this->_price = $this->_defPrice;
			if( file_exists($this->_cfgPath) ){
// 				$data = include($this->_cfgPath);
				$data = file_get_contents($this->_cfgPath);
				if ( !empty($data) ){
					$this->_price = json_decode($data,true);
				}
			}
		}
	}
}

